package dao

import models.Tables._
import play.api.db.slick.{DatabaseConfigProvider, HasDatabaseConfigProvider}
import slick.jdbc.JdbcProfile

import javax.inject.{Inject, Singleton}
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
@Singleton
class SampleIdDao @Inject()(protected val dbConfigProvider: DatabaseConfigProvider) extends
  HasDatabaseConfigProvider[JdbcProfile] {


  import utils.MyPostgresProfile.api._
  import com.github.tototoshi.slick.MySQLJodaSupport._

  type MyRow = SampleIdRow
  val table = SampleId

  def insert(row: MyRow) = {
    db.run(table += row)
  }

  def selectOp(sampleId:String) = db.run(table.
    filter(_.id === sampleId).result.headOption)

  def selectAll: Future[Seq[MyRow]] = db.run(table.sortBy(_.downloadTime.desc).result)

  def selectAllId = db.run(table.map(_.id).result.map(_.toList))

  def updates(rows: Seq[MyRow]): Future[Unit] = {
    val action = DBIO.sequence(
      rows.map { row =>
        table.insertOrUpdate(row)
      }
    ).transactionally
    db.run(action).map(_ => ())
  }

  def delete(id: String): Future[Unit] = db.run(table.filter(_.id === id).delete).map(_ => ())

  def deletes(ids: List[String]): Future[Unit] = db.run(table.filter(_.id.inSetBind(ids)).delete).map(_ => ())


}
